Revit二次开发 您所在的位置:网站首页 revit导出ifc 无反应 Revit二次开发

Revit二次开发

2023-10-20 15:28| 来源: 网络整理| 查看: 265

Revit二次开发——不启动Revit,做rvt文件数据导出 Node.js部分调用C#端供外部调用的dll C#部分调用RevitNet.dll,做数据导出exe做外部调用dll 总结

Node.js部分 调用C#端供外部调用的dll

由于.Net 的WebSevice在引用RevitNet.dll是出现环境不匹配的问题,这里我们的web服务端是用node.js来做的,通过引用edge.js模块来实现在node.js中调用C#的dll,代码如下:

exportModel: async function(file, params){ try { let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExternalReference/bin/Debug/ExternalReference.dll'); //let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModelControl/ExportModelControl/bin/Debug/ExportModelControl.dll'); //let exePath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExportModel/bin/Debug/ExportModel.exe') /**模型源文件存储路径 */ let modelSavePath = sails.config.custom.model_sourcefilesPath; if (!fs.existsSync(modelSavePath)) await fs.mkdirSync(modelSavePath); let thisUpload = await fileHandleUtil.uploadSync(file, { dirname: modelSavePath }); let modelPath = path.join(sails.config.custom.model_sourcefilesPath, path.basename(thisUpload[0].fd)); var invokedll = null; // console.log("exe路径:" + exePath); console.log("模型源文件路径:" + modelPath); //console.log("导出文件路径:" + exePath); if (fs.existsSync(dllPath)) { invokeDll = edge.func({ assemblyFile: dllPath, typeName: 'ExternalReference.Program', methodName: 'Export' }) }else{ assertUtil(null, 'dll path does exist!'); } var result = null; if(invokedll!= null){ result = await invokedll(modelPath, true); }else{ assertUtil(null, 'invokedll miss!'); }; result = result.replace(/\r\n/g,""); var arr = result.split(','); return { modelId: arr[0], modelPath: arr[1] }; } catch (err) { throw err; } } C#部分 调用RevitNet.dll,做数据导出exe namespace ExportModel { public class Startup { static readonly string[] searchs = RevitProductUtility.GetAllInstalledRevitProducts().Select(x => x.InstallLocation).ToArray(); static Startup() { AddEnvironmentPaths(searchs); AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; } [STAThread] static void Main(string[] args) { try { Product _product = Product.GetInstalledProduct(); var clientId = new ClientApplicationId(Guid.NewGuid(), "RevitNetTest", "TEST"); _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality."); Autodesk.Revit.ApplicationServices.Application _application = _product.Application; //string _modelPath = @"H:/Models/TestHouse.rvt"; string _modelPath = args[0]; Document _doc = _application.OpenDocumentFile(_modelPath); List views = new List(from View3D v in new FilteredElementCollector(_doc).OfClass(typeof(View3D)) where v.CanBePrinted && !v.IsTemplate select v); Export export = new Export(_doc, views, "", views[0]); UploadModel upload = new UploadModel(export.modelPath); string result = upload.modelId + "," + upload.modelPath; Console.WriteLine(result); Environment.Exit(0); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// /// 向系统中添加环境变量 /// /// static void AddEnvironmentPaths(params string[] paths) { try { var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty }; var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths)); Environment.SetEnvironmentVariable("PATH", newPath); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// /// 重新加载dll /// /// /// /// private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { try { var assemblyName = new AssemblyName(args.Name); foreach (var item in searchs) { var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name)); if (File.Exists(file)) { return Assembly.LoadFile(file); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return args.RequestingAssembly; } } } 做外部调用dll namespace ExternalReference { public class Program { static Program() { } public async Task Export(string modelPath) { Process process = new Process(); string path = System.IO.Directory.GetCurrentDirectory(); string exePath = Path.Combine(path, @"assets\ExportModel\ExportModel\bin\Debug\ExportModel.exe"); var result = CreateProcess(exePath, modelPath, process); return result; } public string CreateProcess(string exePath, string modelPath, Process process) { string msg = ""; if(File.Exists(exePath)) { process.StartInfo.FileName = exePath; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.Arguments = modelPath; //MessageBox.Show("exe文件路径:" + exePath); //MessageBox.Show("模型文件路径:" + modelPath); process.Start(); msg = process.StandardOutput.ReadToEnd(); process.WaitForExit(); process.Close(); } return msg; } } } 总结

我尝试过把导出方法封装成dll,直接通过node.js或.net WebServer在外部调用。这样做Document _doc = _application.OpenDocumentFile(_modelPath);无法获取到doc,从而无法完成导出。后来只能尝把导出方法封装成一个exe,再通过一个中间的dll去调用exe,获取到了doc。

如果大家有更好的思路或解决方案,欢迎交流!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有